package com.hailiao.beidou.message.data.setting

import com.d10ng.app.resource.getCachePath
import com.d10ng.app.resource.writeFile
import com.d10ng.common.log.LogManager
import com.d10ng.datelib.curTime
import com.d10ng.datelib.toDateStr
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * 日志数据仓库
 * @Author d10ng
 * @Date 2024/3/27 11:42
 */
object LogRepository {

    private val scope = CoroutineScope(Dispatchers.IO)
    // 日志写入路径
    private val logPath by lazy { getCachePath() }

    init {
        scope.launch {
            LogManager.dataFlow.collect {
                // 写入日志文件
                val path = getTodayLogFilePath()
                writeFile(path, "[${it.first.toDateStr()}] ${it.second}\n")
            }
        }
    }

    fun start() {
        println("日志数据仓库启动")
    }

    /**
     * 获取今天的日志文件路径
     * @return String?
     */
    private fun getTodayLogFilePath(): String {
        return "$logPath/log-${curTime.toDateStr("yyyy-MM-dd")}.log"
    }
}